/*----------------------------------------------------------------------------
Owner(s): Shane Whitfield
$Copyright: $
$Id: 

Purpose  : 
----------------------------------------------------------------------------*/

#pragma once

#include "singleton.h"
#include "subsystem.h"
#include "go_playerunits.h"
#include <vector>
#include "level.h"
#include "visrep.h"

/*--------------------------------------------------------------------------*/
// Class CWorld declaration.
class CWorld : public TSingleton<CWorld>, public CCommandTaker, public CSubsystem
{
	typedef CSubsystem super;

public:
					CWorld();
					
	//CCommandTaker Overrides
	virtual const char*	GetName() const {
							return "world"; }
	virtual void		On_Command(const char* i_command, const char* i_parameters);


	//subsystem overrides
	virtual void	OnInit();
	
	void			PostLoad();
	void			Unload();
	void			Update();
	void			Render();
	void			RequestLoad(const char* i_fileName);		
	void			RequestReload();							
	void			LoadPendingWorld();
	const char*		GetFilename() const { 
						return *m_fileName ? m_fileName : NULL; }
	bool			HasPendingWorldRequest() const {
						return *m_requestedFileName && m_isLoadRequested; }
	void			SetLevelFailed() {
						m_wasLevelFailed = true; }
						
	void			SetAbsMin(const Vector2f& i_absMin) {
						m_absMin = i_absMin; }
	void			SetAbsMax(const Vector2f& i_absMax) {
						m_absMax = i_absMax; }
	const Vector2f&	GetAbsMin() const {
						return m_absMin; }
	const Vector2f&	GetAbsMax() const {
						return m_absMax; }
	void			SetMin(const Vector2f& i_min) {
						m_min = i_min; }
	void			SetMax(const Vector2f& i_max) {
						m_max = i_max; }
	const Vector2f&	GetMin() const {
						return m_min; }
	const Vector2f&	GetMax() const {
						return m_max; }
    
    bool            IsLoaded() {
						return m_state == EState_Loaded; }
	
private:

	enum EState
	{
		EState_Loading,
		EState_Loaded,
		EState_LevelComplete,
		EState_LevelFailed,
		EState_Unloading,
	};
	
	class CWorldBackground
	{
	public:
		void			SetTexture(const CTexture* i_texture) {
							m_texture = i_texture; }
		void			SetWorldMat(const Vector2f& i_min, const Vector2f& i_max, EDepthLayer i_depthLayer);
		void			Render();
		
	private:
		
		Vector2f		m_min;
		Vector2f		m_max;
		const CTexture*	m_texture;
		Transform2D		m_worldMat;
	};
	
	void					setState(EState i_state);
	void					initBackGround();

	EState					m_state;
	Vector2f				m_screenTouchPos;
	CWorldBackground		m_worldBackgrounds[2];
	char					m_fileName[100];
	char					m_requestedFileName[100];
	bool					m_isLoadRequested;
	CLevel*					m_level;
	bool					m_wasLevelFailed;
	Vector2f				m_absMin;			// the absolute min of the world (no objects or camera)
	Vector2f				m_absMax;			// the absolute max of the world (no objects or camera)
	Vector2f				m_min;				// the min of the world (camera slides back to this)
	Vector2f				m_max;				// the max of the world (camera slides back to this)
	
	
};
inline CWorld& CWorld_GetSingleton() { return CWorld::GetSingleton(); }
#define g_world (CWorld_GetSingleton())

/*-----------------------------------EOF!-----------------------------------*/